<template>
  <el-dialog
    :title="dialogType ==='dialog_add'? text_add : text_update"
    :close-on-click-modal="isDebug"
    :visible.sync="dialogVisible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm"  label-width="130px" label-position="left" >
#foreach($column in $columns)
#if($column.columnName != $pk.columnName)
      <el-form-item label="${column.comments}" prop="${column.attrname}">
        <el-input v-model="dataForm.${column.attrname}" placeholder="${column.comments}"></el-input>
      </el-form-item>
#end
#end
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="dialogType ==='dialog_add'?requestAdd():requestUpdate()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        dialogVisible: false,
        tempDataFormForm: null,
        dialogType: 'dialog_add', // 弹窗类型 1添加 2编辑
        dataForm: {
#foreach($column in $columns)
#if($column.columnName == $pk.columnName)
      ${column.attrname}: 0,
        #else
      ${column.attrname}: ''#if($velocityCount != $columns.size()),#end

#end
#end
        },
        dataRule: {
          #foreach($column in $columns)
          #if($column.columnName != $pk.columnName)
          ${column.attrname}: [
            { validator: this.checkText, trigger: 'change' }
          ]#if($velocityCount != $columns.size()),#end
          #end
          #end
        }
      }
    },
    props: {
      rowData: {
        type: Object,
        default: null
      }
    },
    methods: {
      initDialog (dialogType) {
        this.dialogVisible = true
        this.dialogType = dialogType
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (dialogType === 'dialog_update') {
            this.requestInfo()
          }
        })
      },
      /** 请求信息(表单回显) */
      requestInfo () {
        this.$httpRequest({
          url: `/${moduleName}/${pathName}/info/${this.rowData.id}`,
          method: 'get'
        }).then((data) => {
          #foreach($column in $columns)
          #if($column.columnName != $pk.columnName)
          this.dataForm.${column.attrname} = data.${classname}.${column.attrname}
          #end
          #end
        }, error => {
          this.showErrorToast(error.msg)
        })
      },
      /** 请求添加 */
      requestAdd () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$httpRequest({
              url: `/${moduleName}/${pathName}/add`,
              method: 'post',
              data: {
            #foreach($column in $columns)
            #if($column.columnName == $pk.columnName)
            '${column.attrname}': this.dataForm.${column.attrname} || undefined,
              #else
            '${column.attrname}': this.dataForm.${column.attrname}#if($velocityCount != $columns.size()),#end

            #end
            #end
              }
            }).then(() => {
              this.dialogVisible = false
              this.showSuccessToast(this.toast_text_add_success, () => {
                this.$emit('refreshDataList')
              })
            }, error => {
              this.showErrorToast(error.msg)
            })
          }
        })
      },
      /** 请求更新 */
      requestUpdate () {
        console.log('requestUpdate')
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$httpRequest({
              url: `/${moduleName}/${pathName}/update`,
              method: 'post',
              data: {
            #foreach($column in $columns)
            #if($column.columnName == $pk.columnName)
            '${column.attrname}': this.dataForm.${column.attrname} || undefined,
              #else
            '${column.attrname}': this.dataForm.${column.attrname}#if($velocityCount != $columns.size()),#end

            #end
            #end
              }
            }).then(() => {
              this.dialogVisible = false
              this.showSuccessToast(this.toast_text_update_success, () => {
                this.$emit('refreshDataList')
              })
            }, error => {
              this.showErrorToast(error.msg)
            })
          }
        })
      }
    }
  }
</script>
